package com.zx.cloud.security;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5
 */
public class MD5Util {

	private static final char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

	private MD5Util() {
	}

	public static String getMD5(String source) throws NoSuchAlgorithmException {
		if (null == source)
			return source;
		String s = null;
		MessageDigest md = MessageDigest.getInstance("MD5");
		md.update(source.getBytes());
		byte tmp[] = md.digest();
		char str[] = new char[16 * 2];
		int k = 0;
		for (int i = 0; i < 16; i++) {
			byte byte0 = tmp[i];
			str[k++] = hexDigits[byte0 >>> 4 & 0xf];
			str[k++] = hexDigits[byte0 & 0xf];
		}
		s = new String(str);
		return s;
	}

	public static String sign(String content, String key) throws Exception {
		String signData = content + "&" + key;
		return getMD5(signData);
	}

	public static boolean checkSign(String content, String sign, String key) throws Exception {
		String signData = sign(content, key);
		return signData.equals(sign);
	}

}